package com.irdstudio.efp.riskm.service.impl;

import com.irdstudio.basic.framework.core.util.TimeUtil;
import com.irdstudio.efp.console.common.ConsoleConstant;
import com.irdstudio.efp.esb.service.bo.req.sed.letter.ReqSedLetterBean;
import com.irdstudio.efp.esb.service.facade.sed.letter.SedLetterInformService;
import com.irdstudio.efp.flow.common.vo.PageApproveVO;
import com.irdstudio.efp.flow.service.facade.BizEventInfoService;
import com.irdstudio.efp.riskm.service.dao.ColltSmsNoticeAppDao;
import com.irdstudio.efp.riskm.service.dao.ColltTaskInfoDao;
import com.irdstudio.efp.riskm.service.domain.ColltSmsNoticeApp;
import com.irdstudio.efp.riskm.service.domain.ColltTaskInfo;
import com.irdstudio.efp.riskm.service.facade.ColltSmsNoticeAppApprService;
import com.irdstudio.efp.riskm.service.vo.ColltSmsNoticeAppVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Objects;

import static com.irdstudio.basic.framework.core.util.BeanUtility.beanCopy;


/**
 * Service Impl: 催收短信通知申请表服务实现
 * @author ligm
 * @date 2019-08-27
 */
@Service("colltSmsNoticeAppApprService")
public class ColltSmsNoticeAppApprServiceImpl implements ColltSmsNoticeAppApprService {

	private static Logger logger = LoggerFactory.getLogger(ColltSmsNoticeAppApprServiceImpl.class);

	@Autowired
	private ColltSmsNoticeAppDao colltSmsNoticeAppDao;
	@Autowired
	@Qualifier("bizEventInfoService")
	private BizEventInfoService bizEventInfoService;
	@Autowired
	private ColltTaskInfoDao colltTaskInfoDao;

	@Autowired
	@Qualifier("sedLetterInformService")
	private SedLetterInformService sedLetterInformService;

	@Override
	public void pass(String bizSerno, PageApproveVO pageApproveVo) {
		logger.info("专项催收短信通知通过流水号" + bizSerno);
		try {
			if (Objects.nonNull(bizSerno) && !"".equals(bizSerno)) {
				ColltSmsNoticeAppVO vo = new ColltSmsNoticeAppVO();
				vo.setSmsNoticeApp(bizSerno);
				ColltSmsNoticeApp colltSmsNoticeApp = new ColltSmsNoticeApp();
				beanCopy(vo,colltSmsNoticeApp);
				colltSmsNoticeApp= colltSmsNoticeAppDao.queryByPk(colltSmsNoticeApp);
				beanCopy(colltSmsNoticeApp,vo);
				if (Objects.nonNull(vo)) {
					vo.setLastUpdateTime(TimeUtil.getCurrentDateTime());
					vo.setLastUpdateUser(pageApproveVo.getAprvUserId());
					// 审批用户编号
					vo.setAprvUserCode(pageApproveVo.getAprvUserId());
					// 审批意见
					vo.setAprvComment(pageApproveVo.getAprvComment());
					// 审批状态 = 通过
					vo.setApproveStatus(ConsoleConstant.APRV_STATUS_03);
					// 审批时间
					vo.setAprvTime(TimeUtil.getCurrentDateTime());
				}
				beanCopy(vo, colltSmsNoticeApp);
				colltSmsNoticeAppDao.updateByPk(colltSmsNoticeApp);

				//通过后任务状态改为待处理
				ColltTaskInfo colltTaskInfo = new ColltTaskInfo();
				colltTaskInfo.setColltTaskNo(colltSmsNoticeApp.getColltTaskNo());
				ColltTaskInfo tS = colltTaskInfoDao.queryByPk(colltTaskInfo);
				tS.setTaskSts(ConsoleConstant.APRV_STATUS_01);
				colltTaskInfoDao.updateByPk(tS);

				//调用短信接口，通知客户
				this.sendMessageToCus(colltSmsNoticeApp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void refuse(String bizSerno, PageApproveVO pageApproveVo) {
		logger.info("专项催收短信通知拒绝流水号" + bizSerno);
		try {
			if (Objects.nonNull(bizSerno) && !"".equals(bizSerno)) {
				ColltSmsNoticeAppVO vo = new ColltSmsNoticeAppVO();
				vo.setSmsNoticeApp(bizSerno);
				ColltSmsNoticeApp colltSmsNoticeApp = new ColltSmsNoticeApp();
				beanCopy(vo,colltSmsNoticeApp);
				colltSmsNoticeApp= colltSmsNoticeAppDao.queryByPk(colltSmsNoticeApp);
				beanCopy(colltSmsNoticeApp,vo);
				if (Objects.nonNull(vo)) {
					vo.setLastUpdateTime(TimeUtil.getCurrentDateTime());
					vo.setLastUpdateUser(pageApproveVo.getAprvUserId());
					// 审批用户编号
					vo.setAprvUserCode(pageApproveVo.getAprvUserId());
					// 审批意见
					vo.setAprvComment(pageApproveVo.getAprvComment());
					// 审批状态 = 通过
					vo.setApproveStatus(ConsoleConstant.APRV_STATUS_04);
					// 审批时间
					vo.setAprvTime(TimeUtil.getCurrentDateTime());
				}
				beanCopy(vo, colltSmsNoticeApp);
				colltSmsNoticeAppDao.updateByPk(colltSmsNoticeApp);

				//拒绝后任务状态改为待处理
				ColltTaskInfo colltTaskInfo = new ColltTaskInfo();
				colltTaskInfo.setColltTaskNo(colltSmsNoticeApp.getColltTaskNo());
				ColltTaskInfo tS = colltTaskInfoDao.queryByPk(colltTaskInfo);
				tS.setTaskSts(ConsoleConstant.APRV_STATUS_01);
				colltTaskInfoDao.updateByPk(tS);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void repulse(String bizSerno, PageApproveVO pageApproveVo) {
		try{
			logger.info("专项催收短信通知打回流水号" + bizSerno);
			if(bizEventInfoService.isFirstNode(bizSerno)){
				// 更新打回状态
				if (Objects.nonNull(bizSerno) && !"".equals(bizSerno)) {
					ColltSmsNoticeAppVO vo = new ColltSmsNoticeAppVO();
					vo.setSmsNoticeApp(bizSerno);
					ColltSmsNoticeApp colltSmsNoticeApp = new ColltSmsNoticeApp();
					beanCopy(vo,colltSmsNoticeApp);
					colltSmsNoticeApp= colltSmsNoticeAppDao.queryByPk(colltSmsNoticeApp);
					beanCopy(colltSmsNoticeApp,vo);
					if (Objects.nonNull(vo)) {
						vo.setLastUpdateTime(TimeUtil.getCurrentDateTime());
						vo.setLastUpdateUser(pageApproveVo.getAprvUserId());
						// 审批用户编号
						vo.setAprvUserCode(pageApproveVo.getAprvUserId());
						// 审批意见
						vo.setAprvComment(pageApproveVo.getAprvComment());
						// 审批状态 = 通过
						vo.setApproveStatus(ConsoleConstant.APRV_STATUS_05);
						// 审批时间
						vo.setAprvTime(TimeUtil.getCurrentDateTime());
					}
					beanCopy(vo, colltSmsNoticeApp);
					colltSmsNoticeAppDao.updateByPk(colltSmsNoticeApp);
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	private void sendMessageToCus(ColltSmsNoticeApp colltSmsNoticeApp){
		logger.info("======>专项催收申请审批通过后，调用短信通知接口开始<======");
		ReqSedLetterBean reqSedLetterBean = new ReqSedLetterBean();
		reqSedLetterBean.setMblNo(colltSmsNoticeApp.getPhoneNum());
		reqSedLetterBean.setCntntInf(colltSmsNoticeApp.getSendMsg());
		try {
			sedLetterInformService.sendLetter(reqSedLetterBean);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("======>专项催收申请审批通过后，调用短信通知接口失败<======" + e.getMessage());
		} finally {
			logger.info("======>专项催收申请审批通过后，调用短信通知接口开始<======");
		}
	}

}
